<template>
  <div class="login">
    <div class="login-f"></div>
    <el-form ref="loginForm" v-if="!isr" :model="loginForm" :rules="loginRules" class="login-form">
      <img src="../assets/images/or.jpg" class="ys" @click="isr = true"/>
      <div class="container"><img src="../assets/images/slice.jpg" class="image"/>
        <p class="title">
          项目管理系统</p>
      </div>
      <p class="sontitle">欢迎使用青墨湾项目管理系统</p>

      <el-form-item prop="username" style="margin-top: 50px;">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="请输入账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>

      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;height: 44px"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>

        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>

        <!--  底部  -->
        <div class="el-login-footer">
          <span>Copyright © 2015-2021 浙ICP备2021036742号</span>
        </div>
      </el-form-item>
    </el-form>


    <el-form ref="loginForm" v-if="isr" :model="loginForm" :rules="loginRules" class="login-formR">
      <div class="container" >
        <img src="../assets/images/or1.jpg" class="ys" @click="isr = false"/>
        <p class="title">
          <img src="../assets/images/slice.jpg" class="img"/>
          项目管理系统</p>
      </div>
      <div class="container" >
      <p class="sontitle" >欢迎使用青墨湾项目管理系统</p></div>

      <div class="image-container">
      <img src="../assets/images/rwm.jpg" class="rwm"/>
      </div>

      <div class="container" >
        <p class="sontitle" >使用客户端扫描二维码登录</p></div>

      <el-form-item style="width:100%;">

        <!--  底部  -->
        <div class="el-login-footer">
          <span>Copyright © 2015-2021 浙ICP备2021036742号</span>
        </div>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        //写死
        username: process.env.VUE_APP_USERNAME,
        password: process.env.VUE_APP_PASSWORD,
        rememberMe: false,
        // code: "",
        // uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: false,
      redirect: undefined,
      isr: false
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("LoginNoCode", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            // if (this.captchaEnabled) {
            //   this.getCode();
            // }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  font-size: 20px;
  font-family: Microsoft YaHei-Bold, Microsoft YaHei;
  font-weight: bold;
  color: #4B4B4B;
  line-height: 0px;
}
.title img {
  margin-right: 0.5rem; /* 调整图片和文字之间的水平间距 */
}
.login-f{
  border-radius: 8px;
  background: #ffffff;
  height: 60%;
  width: 25%;
  background-image: url("../assets/images/background.jpg");
  background-size: cover;
}
.sontitle {
  height: 8%;
  width: 156px;
  font-size: 12px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
  font-weight: 400;
  color: #6A6A6A;
  line-height: 0px;
}
.image-container {
  display: flex;
  justify-content: center;
}
.rwm {
  padding-top: 5%;
  padding-bottom: 4%;
  width: 80%; /* 设置合适的百分比值来缩小图片的宽度 */
  height: auto; /* 让高度自适应以保持图片比例 */

}

/**
白登录框
 */
.login-form {
  position: relative;  /* 添加相对定位 */
  border-radius: 8px;
  background: #ffffff;
  height: 60%;
  width: 30%;

  padding-top: 3.5%;
  padding-left: 6.5%;
  padding-right: 6.5%;
  //padding-bottom: 10%;

  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); /* 添加外阴影 */

  .ys{
    position: absolute; /* 添加绝对定位 */
    width: 13%;
    height: 13%;
    top: 0; /* 将元素置于顶部 */
    right: 0; /* 将元素置于右侧 */
    margin-top: 1px; /* 可根据需要调整元素与顶部的间距 */
    margin-right: 1px; /* 可根据需要调整元素与右侧的间距 */
    cursor: pointer;
  }
  .container {
    display: flex;
    align-items: center;
    .image {
      margin-right: 10px; /* 调整图片与文字之间的间距 */
    }
  }
  .el-input {
    //position: absolute;
    height: 36px;
    input {
      height: 36px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

//r
.login-formR {
  position: relative;  /* 添加相对定位 */
  border-radius: 8px;
  background: #ffffff;
  height: 60%;
  width: 30%;

  padding-top: 3.5%;
  padding-left: 6.5%;
  padding-right: 6.5%;

  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); /* 添加外阴影 */

  .ys{
    position: absolute; /* 添加绝对定位 */
    width: 13%;
    height: 13%;
    top: 0; /* 将元素置于顶部 */
    right: 0; /* 将元素置于右侧 */
    margin-top: 1px; /* 可根据需要调整元素与顶部的间距 */
    margin-right: 1px; /* 可根据需要调整元素与右侧的间距 */
    cursor: pointer;
  }
  .container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
  }

}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  position: absolute;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  height: 25%;
  text-align: center;
  color: #C0C4CC;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
  width: 100%;
}
</style>
